package com.zyh.stream;

import com.zyh.domain.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zyh
 * @Date 2024/8/16 11:38
 * @Description 聚合
 */

public class StreamDemo4 {
    @Test
    public void test1() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zyh", "男", 18, 170.5));
        list.add(new Student("wy", "男", 20, 140.5));
        list.add(new Student("cxk", "女", 45, 120.5));
        list.add(new Student("dz", "女", 2, 180.5));
        list.add(new Student("zhangSan", "男", 13, 146.5));
        list.add(new Student("li4", "女", 56, 189.5));
        list.add(new Student("wang5", "男", 24, 110.5));
        //求出集合元素中最高的身高
        Double height = list.stream().max((item1, item2) -> (int) (item1.getHeight() - item2.getHeight())).get().getHeight();
        System.out.println("height = " + height);
        //求出集合元素中最小年龄
        Integer age = list.stream().min((item1, item2) -> item1.getAge() - item2.getAge()).get().getAge();
        System.out.println("age = " + age);
        //求出男生中最高的身高
        Double height1 = list.stream().filter(item -> "男".equals(item.getSex())).max((item1, item2) -> (int) (item1.getHeight() - item2.getHeight())).get().getHeight();
        System.out.println("height1 = " + height1);
        //求出身高大于170的学生的最小年龄
        Integer age1 = list.stream().filter(item -> item.getHeight() > 170.0).min((item1, item2) -> item1.getAge() - item2.getAge()).get().getAge();
        System.out.println("age1 = " + age1);
        //统计元素总个数
        long count = list.stream().count();
        System.out.println("count = " + count);
        //统计女生人数
        long count1 = list.stream().filter(item -> "女".equals(item.getSex())).count();
        System.out.println("count1 = " + count1);
        //统计身高大于170男生人数
        long count2 = list.stream().filter(item -> "男".equals(item.getSex()) && item.getHeight() > 170.0).count();
        System.out.println("count2 = " + count2);
    }
}
